<?php
class Cart_Model_WebPay extends Ext_Db_Table
{
	protected $_name = "pg_webpay";
	
	public function saveTransaction( $transaction )
	{
	    $writer = new Zend_Log_Writer_Stream(
	    	Zend_Registry::get('config')->webpay->log->transaction
	    );
	    $log = new Zend_Log( $writer );

        // guarda registro con datos de webpay
 		$data['tbk_anio_transaccion'] = date("Y");
 		
        $data = array(
			'orden_compra' =>  $transaction['TBK_ORDEN_COMPRA'],
            'tipo_transaccion' =>  $transaction['TBK_TIPO_TRANSACCION'],
            'tbk_monto' => $transaction['TBK_MONTO'],
            'codigo_autorizacion' => $transaction['TBK_CODIGO_AUTORIZACION'],
            'final_numero_tarjeta' => $transaction['TBK_FINAL_NUMERO_TARJETA'],
            'tbk_tipo_pago' => $transaction['TBK_TIPO_PAGO'],
            'tbk_numero_cuotas' => $transaction['TBK_NUMERO_CUOTAS'],
            'tbk_respuesta' => $transaction['TBK_RESPUESTA'],
            'tbk_fecha_contable' => $transaction['TBK_FECHA_CONTABLE'],
            'tbk_fecha_transaccion' => $transaction['TBK_FECHA_TRANSACCION'],
            'tbk_hora_transaccion' => $transaction['TBK_HORA_TRANSACCION'],
            'tbk_id_sesion' => $transaction['TBK_ID_SESION'],
            'tbk_id_transaccion' => $transaction['TBK_ID_TRANSACCION'],
            'tbk_tasa_interes_max' => $transaction['TBK_TASA_INTERES_MAX'],
            'tbk_mac' => $transaction['TBK_MAC']
		);  

		$row = $this->find( $transaction['TBK_ORDEN_COMPRA'] )->current();

        if ( count( $row ) == 0 ) {
	        $row = $this->createRow();
	    }

		//Guardamos el detalle en la base
		$row->setFromArray( $data );
		$row->save();

        $log->info("Insertando orden_id " . $data['orden_compra']
        	." trans_id ".$data['tbk_id_transaccion']
        	." aut_code ".$data['codigo_autorizacion']
        );
	}

    public function getTransaction( $orderId )
    {
        $rowset = $this->fetchAll(
            $this->select()
            ->where( 'orden_compra = ?', $orderId )
        );
        if( count( $rowset ) ) {
            return $rowset;
        }
        return array();
    }

}
